﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Windows.Forms;

namespace CSharpTestProject.Utils
{
    internal class Utils
    {
        private static readonly string path = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) + "\\userInfo.m";
        public static void RememberAccount(string logName, string password,bool is_teacher)
        {

            FileInfo info = new FileInfo(path);
            if (!info.Exists)
            {
                info.Create().Close();
            }
            //info.Attributes = FileAttributes.Hidden;
            //Pass the filepath and filename to the StreamWriter Constructor
            StreamWriter sw = new StreamWriter(path);
            if (logName == "" && password == "")
            {
                sw.Write("");
                sw.Close();
                return;
            }
            //Write a line of text
            sw.WriteLine(logName);
            //Write a second line of text
            sw.WriteLine(password);
            sw.WriteLine(is_teacher?"0":"1");
            //Close the file
            sw.Close();

        }
        public static string[] FindAccount()
        {
            try
            {
                StreamReader sr = new StreamReader(path);
                string[] ret = new string[3];
                ret[0] = sr.ReadLine();
                ret[1] = sr.ReadLine();
                ret[2] = sr.ReadLine();
                sr.Close();
                return ret;
            }
            catch
            {
                return null;
            }
        }
        public static Student[] GetRandomStudents(Student[] students, int num)
        {
            if (students == null) return null;
            if (num == 0) num = students.Length;
            Random random = new Random();
            Student[] students1 = new Student[num];
            List<Student> students2 = students.ToList();
            if (students2.Count < num) num = students2.Count;
            for (int i = 0; i < num; i++)
            {
                if (students2.Count <= 0) { break; }
                int r = random.Next(students2.Count);
                students1[i] = students2[r];
                students2.RemoveAt(r);
            }
            return students1;
        }
        public static string[] DelArraySame(string[] TempArray)
        {
            ArrayList nStr = new ArrayList();
            for (int i = 0; i < TempArray.Length; i++)
            {
                if (!nStr.Contains(TempArray[i]))
                {
                    nStr.Add(TempArray[i]);
                }
            }
            string[] newStr = (string[])nStr.ToArray(typeof(string));
            return newStr;
        }
        public static DataTable GirdViewToDataTable(DataGridView dv)
        {
            DataTable dt = new DataTable();
            DataColumn dc;
            for (int i = 0; i < dv.Columns.Count; i++)
            {
                dc = new DataColumn();
                dc.ColumnName = dv.Columns[i].HeaderText.ToString();
                dt.Columns.Add(dc);
            }
            for (int j = 0; j < dv.Rows.Count; j++)
            {
                DataRow dr = dt.NewRow();
                for (int x = 0; x < dv.Columns.Count; x++)
                {
                    dr[x] = dv.Rows[j].Cells[x].Value;
                }
                dt.Rows.Add(dr);
            }
            return dt;
        }

    }
}
